import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _set from "lodash/set";
import _startsWith from "lodash/startsWith";
import _isFunction from "lodash/isFunction";
import _forEach from "lodash/forEach";

var _dec, _dec2, _dec3, _dec4, _class, _class2;

/**
 * FieldProp.js
 * @date 2018-01-07
 */
import React from 'react';
import { Form, Input, Divider, InputNumber, Button } from 'antd';
import { Bind } from 'lodash-decorators';
import { modalBtnPrefix, subEventPrefix } from 'components/DynamicComponent/config';
import ValueList from 'components/ValueList';
import intl from 'utils/intl';
import { attributeNameProp, attributeTypeProp, attributeValueProp } from "../../config";
import ButtonEditModal from "../ScriptEdit/ButtonEditModal";
var inputComponentStyle = {
  width: '100%'
};
var TOOLBAR_BTN_PREFIX = '[btn]';
var actionStyle = {
  width: '100%',
  marginTop: 8,
  marginBottom: 8
};
var FieldProp = (_dec = Form.create({
  fieldNameProp: null,
  onValuesChange: function onValuesChange(props, changedValues, allValues) {
    if (_isFunction(props.onValuesChange)) {
      props.onValuesChange(props, changedValues, allValues);
    }
  }
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(FieldProp, _React$Component);

  var _super = _createSuper(FieldProp);

  function FieldProp() {
    var _this;

    _classCallCheck(this, FieldProp);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {};
    return _this;
  }

  _createClass(FieldProp, [{
    key: "render",
    value: // 防止第一次 state 是空报错
    function render() {
      var _this$props = this.props,
          field = _this$props.field,
          form = _this$props.form;
      var propValues = {};

      _forEach(field.config, function (prop) {
        propValues[prop.attributeName] = prop.value;
      });

      var btnConfigs = {};
      var btnProps = {
        btn: {}
      };

      _forEach(field.config, function (prop) {
        if (_startsWith(prop[attributeNameProp], TOOLBAR_BTN_PREFIX)) {
          // 设置 btn 的属性
          if (_startsWith(prop[attributeNameProp], "".concat(TOOLBAR_BTN_PREFIX).concat(modalBtnPrefix))) {
            btnConfigs[(prop[attributeNameProp] || '').substr(TOOLBAR_BTN_PREFIX.length)] = prop[attributeTypeProp];

            if (btnProps.btn.modalBtns) {
              btnProps.btn.modalBtns.push(prop);
            } else {
              btnProps.btn.modalBtns = [prop];
            }
          } else if (_startsWith(prop[attributeNameProp], "".concat(TOOLBAR_BTN_PREFIX).concat(subEventPrefix))) {
            btnConfigs[(prop[attributeNameProp] || '').substr(TOOLBAR_BTN_PREFIX.length)] = prop[attributeTypeProp];

            if (btnProps.btn.subEvents) {
              btnProps.btn.subEvents.push(prop);
            } else {
              btnProps.btn.subEvents = [prop];
            }
          } else {
            // 设置除了 modalBtn, subEvent 之外的属性类型
            _set(btnProps, prop[attributeNameProp], prop[attributeValueProp]);
          }
        } else {
          propValues[prop[attributeNameProp]] = prop[attributeValueProp];
        }
      });

      return /*#__PURE__*/React.createElement(Form, null, form.getFieldDecorator('btnConfigs', {
        initialValue: btnConfigs
      })( /*#__PURE__*/React.createElement("div", null)), form.getFieldDecorator('btnProps', {
        initialValue: btnProps.btn
      })( /*#__PURE__*/React.createElement("div", null)), this.renderFieldCommonProps(propValues), /*#__PURE__*/React.createElement(Divider, null), this.renderFieldTypeProps(propValues));
    }
  }, {
    key: "renderFieldCommonProps",
    value: function renderFieldCommonProps() {
      var _this$props2 = this.props,
          form = _this$props2.form,
          field = _this$props2.field;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, {
        key: "fieldName",
        label: "\u7F16\u7801"
      }, form.getFieldDecorator('fieldName', {
        initialValue: field.fieldName,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validate.notNull', {
            name: intl.get('cpfm.ui.field.fieldName').d('编码')
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        disabled: true
      }))), /*#__PURE__*/React.createElement(Form.Item, {
        key: "fieldLabel",
        label: "\u6807\u9898"
      }, form.getFieldDecorator('fieldLabel', {
        initialValue: field.fieldLabel
      })( /*#__PURE__*/React.createElement(Input, null))));
    }
  }, {
    key: "renderFieldTypeProps",
    value: function renderFieldTypeProps(propValues) {
      var field = this.props.field;
      var renderFunc = "renderFieldType".concat(field.componentType, "Props");

      if (_isFunction(this[renderFunc])) {
        return this[renderFunc](propValues);
      }

      return null;
    }
  }, {
    key: "renderFieldTypeButtonProps",
    value: function renderFieldTypeButtonProps() {
      var _this2 = this;

      var propValues = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var form = this.props.form;
      var _this$state = this.state,
          _this$state$buttonEdi = _this$state.buttonEditVisible,
          buttonEditVisible = _this$state$buttonEdi === void 0 ? false : _this$state$buttonEdi,
          _this$state$buttonEdi2 = _this$state.buttonEditProps,
          buttonEditProps = _this$state$buttonEdi2 === void 0 ? {} : _this$state$buttonEdi2;
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, {
        key: "type",
        label: "\u7C7B\u578B"
      }, form.getFieldDecorator('type', {
        initialValue: propValues.type
      })(React.createElement(ValueList, {
        options: [{
          value: 'primary',
          meaning: '主要'
        }, {
          value: 'dashed',
          meaning: '不重要'
        }, {
          value: 'danger',
          meaning: '危险'
        }],
        style: {
          width: '100%'
        }
      }))), /*#__PURE__*/React.createElement(Form.Item, {
        key: "styleMarginRight",
        label: "\u53F3\u95F4\u8DDD"
      }, form.getFieldDecorator('style.marginRight', {
        initialValue: propValues['style.marginRight']
      })( /*#__PURE__*/React.createElement(InputNumber, {
        style: inputComponentStyle
      }))), /*#__PURE__*/React.createElement(Button, {
        key: "action",
        onClick: function onClick() {
          _this2.handleOpenButtonEditModal(propValues);
        },
        style: actionStyle,
        type: "primary"
      }, "\u6267\u884C\u52A8\u4F5C"), buttonEditVisible && /*#__PURE__*/React.createElement(ButtonEditModal, {
        key: "actionModal",
        buttonEditProps: buttonEditProps,
        visible: buttonEditVisible,
        onOk: this.handleButtonEditModalOk,
        onCancel: this.handleButtonEditModalCancel
      }));
    }
  }, {
    key: "handleOpenButtonEditModal",
    value: function handleOpenButtonEditModal() {
      var _this$props3 = this.props,
          _this$props3$config = _this$props3.config,
          config = _this$props3$config === void 0 ? {} : _this$props3$config,
          form = _this$props3.form;
      var btnProps = form.getFieldValue('btnProps');
      this.setState({
        buttonEditVisible: true,
        buttonEditProps: {
          propsValue: btnProps,
          scripts: config.scripts,
          components: config.fields
        }
      });
    }
  }, {
    key: "handleButtonEditModalOk",
    value: function handleButtonEditModalOk(_ref) {
      var validateData = _ref.validateData,
          attrConfig = _ref.attrConfig;
      var form = this.props.form;
      var btnConfigs = {};

      _forEach(attrConfig, function (attr) {
        btnConfigs[attr[attributeNameProp]] = attr[attributeTypeProp];
      });

      form.setFieldsValue({
        btnProps: validateData,
        btnConfigs: btnConfigs
      }); // 关闭模态框

      this.handleButtonEditModalCancel();
    }
  }, {
    key: "handleButtonEditModalCancel",
    value: function handleButtonEditModalCancel() {
      this.setState({
        buttonEditVisible: false,
        buttonEditProps: {}
      });
    }
  }]);

  return FieldProp;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleOpenButtonEditModal", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleOpenButtonEditModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleButtonEditModalOk", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleButtonEditModalOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleButtonEditModalCancel", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleButtonEditModalCancel"), _class2.prototype)), _class2)) || _class);
FieldProp.displayName = 'DynamicTabs(FieldProp)';
export default FieldProp;